package com.rede.didiok.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.admin.dao.SysLogDao;
import com.rede.didiok.admin.entity.SysLogEntity;
import com.rede.didiok.admin.service.SysLogService;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.dto.admin.SysLogDto;
import com.rede.didiok.common.enums.StatusEnum;
import com.rede.didiok.common.utils.DateUtils;
import com.rede.didiok.common.utils.StringUtils;


@Service("sysLogService")
public class SysLogServiceImpl extends ServiceImpl<SysLogDao, SysLogEntity> implements SysLogService {

    @Autowired
    SysLogService sysLogService;

    /***************************** admin:start **************************/
    // region  didiok-admin

    @Override
    public IPage<SysLogEntity> getPageList(SysLogDto sysLogDto) {

        QueryWrapper<SysLogEntity> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(sysLogDto.getUserName())) {
            queryWrapper.eq(SQLConf.USER_NAME, sysLogDto.getUserName().trim());
        }

        if (StringUtils.isNotBlank(sysLogDto.getOperation())) {
            queryWrapper.eq(SQLConf.OPERATION, sysLogDto.getOperation());
        }

        if (StringUtils.isNotBlank(sysLogDto.getIp())) {
            queryWrapper.eq(SQLConf.IP, sysLogDto.getIp());
        }

        if (StringUtils.isNotBlank(sysLogDto.getStartTime())) {
            String[] time = sysLogDto.getStartTime().split(SysConf.FILE_SEGMENTATION);
            if (time.length == Constants.NUM_TWO) {
                queryWrapper.between(SQLConf.CREATE_TIME, DateUtils.str2Date(time[0]), DateUtils.str2Date(time[1]));
            }
        }

        if (StringUtils.isNotBlank(sysLogDto.getSpendTimeStr())) {
            String[] spendTimeList = StringUtils.split(sysLogDto.getSpendTimeStr(), Constants.SYMBOL_UNDERLINE);
            if (spendTimeList.length == Constants.NUM_TWO) {
                queryWrapper.between(SQLConf.SPEND_TIME, Integer.valueOf(spendTimeList[0]), Integer.valueOf(spendTimeList[1]));
            }
        }

        Page<SysLogEntity> page = new Page<>();
        page.setCurrent(sysLogDto.getCurrentPage());
        page.setSize(sysLogDto.getPageSize());
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<SysLogEntity> pageList = sysLogService.page(page, queryWrapper);
        return pageList;
    }
    // endregion
    /***************************** admin:end **************************/



    /***************************** web:start **************************/
    // region didiok-web

    // endregion
    /***************************** web:end **************************/

}